package com.example.work;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

// 外部监听器类

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   private Button b1;
    private Button b2;
    private Button b3;
    private Button b4;
    private Button b5;
    private Button b6;
    private Button b7;
    private Button b8;

    private TextView t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        setupButtonClickListeners();
    }



    // 方式2: 内部类实现 OnClickListener
    private class InnerClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {updateStatusText("当前动作: 内部类监听到点击");}
    }


    private void setupButtonClickListeners() {

        // 1. Activity作为监听器
        b1.setOnClickListener(this); // 'this' 指代实现了 OnClickListener 的 MainActivity

        // 2. 内部类作为监听器
        b2.setOnClickListener(new InnerClickListener());

        // 3. 匿名内部类作为监听器
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateStatusText("当前动作: 匿名内部类点击");
            }
        });

        // 4. LAMBDA表达式作为监听器 (需要 Java 8+)
        b4.setOnClickListener(view -> updateStatusText("当前动作:Lambda表达式监听到点击 "));

        // 5. 外部类作为监听器
        b5.setOnClickListener(new ExternalClickListener(t));


        b7.setOnClickListener(v -> startActivity(new Intent(this,SystemInfoActivity.class)));

        b8.setOnClickListener(v -> startActivity(new Intent(this, ProgressDialogTest.class)));
    }


    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if(viewId == R.id.button){
           t.setText("当前动作: Activity监听");
        }
    }

    public void init(){
        b1 = findViewById(R.id.button);
        b2 = findViewById(R.id.button2);
        b3 = findViewById(R.id.button3);
        b4 = findViewById(R.id.button4);
        b5 = findViewById(R.id.button5);
        b7 = findViewById(R.id.button7);
        b8 = findViewById(R.id.button8);
        t = findViewById(R.id.textView);
    }

    private void updateStatusText(String actionText) {
            t.setText(actionText);

    }

    public void btn2(View v){
        t.setText("当前动作：监听器绑定到标签");
    }
}